home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Games / Connect-4 v3.2 / game.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-05  |  7.3 KB  |  231 lines  |  [TEXT/CWIE]

  1. /****************************************************************************/
  2. /**                                                                        **/
  3. /**                          Connect-4 Algorithm                           **/
  4. /**                                                                        **/
  5. /**                              Version 3.2                               **/
  6. /**                                                                        **/
  7. /**                            By Keith Pomakis                            **/
  8. /**                          (pomakis@pobox.com)                           **/
  9. /**                                                                        **/
  10. /**                               June, 1996                               **/
  11. /**                                                                        **/
  12. /****************************************************************************/
  13. /**                                                                        **/
  14. /**                          Sample Implementation!                        **/
  15. /**                                                                        **/
  16. /**  This code is poorly written and contains no internal documentation.   **/
  17. /**  Its sole purpose is to quickly demonstrate an actual implementation   **/
  18. /**  of the functions contained in the file "c4.c".  It's a fully working  **/
  19. /**  game which should work on any type of system, so give it a shot!      **/
  20. /**                                                                        **/
  21. /**  The computer is pretty brain-dead at level 3 or less, but at level 4  **/
  22. /**  and up it provides quite a challenge!                                 **/
  23. /**                                                                        **/
  24. /****************************************************************************/
  25.  
  26. #include <stdio.h>
  27. #include <ctype.h>
  28. #include "c4.h"
  29.  
  30. #define BUFFER_SIZE 80
  31.  
  32. enum {HUMAN = 0, COMPUTER = 1};
  33.  
  34. static int get_num(char *prompt, int lower, int upper);
  35. static void print_board(int width, int height);
  36. static void print_dot(void);
  37.  
  38. static char piece[2] = { 'X', 'O' };
  39.  
  40. int
  41. main()
  42. {
  43.     int player[2], level[2], turn = 0, num_of_players, move;
  44.     int width, height, num_to_connect;
  45.     int x1, y1, x2, y2;
  46.     char buffer[BUFFER_SIZE];
  47.  
  48.     printf("\n****  Welcome to the game of Connect!  ****\n\n");
  49.     printf("By Keith Pomakis (pomakis@pobox.com)\n");
  50.     printf("June 16, 1994\n\n");
  51.  
  52.     width = get_num("Width of board (7 is standard)? ", 1, 40);
  53.     height = get_num("Height of board (6 is standard)? ", 1, 40);
  54.     num_to_connect = get_num("Number to connect (4 is standard)? ", 1, 40);
  55.  
  56.     num_of_players = get_num("Number of human players (0, 1 or 2)? ", 0, 2);
  57.     switch (num_of_players) {
  58.  
  59.         case 0:
  60.             player[0] = player[1] = COMPUTER;
  61.             level[0] = get_num("Skill level of player X (1 - 10)? ", 1, 10);
  62.             level[1] = get_num("Skill level of player O (1 - 10)? ", 1, 10);
  63.             turn = 0;
  64.             break;
  65.  
  66.         case 1:
  67.             player[0] = HUMAN;
  68.             player[1] = COMPUTER;
  69.             level[1] = get_num("Skill level of computer (1 - 10)? ", 1, 10);
  70.             buffer[0] = '\0';
  71.             do {
  72.                 printf("Would you like to go first? ");
  73.                 if (!fgets(buffer, BUFFER_SIZE, stdin)) {
  74.                     printf("\nGoodbye!\n");
  75.                     exit(0);
  76.                 }
  77.                 buffer[0] = tolower(buffer[0]);
  78.             } while (buffer[0] != 'y' && buffer[0] != 'n');
  79.  
  80.             if (buffer[0] == 'y')
  81.                 turn = 0;
  82.             else
  83.                 turn = 1;
  84.             break;
  85.  
  86.         case 2:
  87.             player[0] = player[1] = HUMAN;
  88.             turn = 0;
  89.             break;
  90.     }
  91.  
  92.     c4_new_game(width, height, num_to_connect);
  93.  
  94.     do {
  95.         print_board(width, height);
  96.         if (player[turn] == HUMAN) {
  97.             do {
  98.                 if (num_of_players == 2)
  99.                     sprintf(buffer, "Player %c, drop in which column? ",
  100.                             piece[turn]);
  101.                 else
  102.                     sprintf(buffer, "Drop in which column? ");
  103.                 move = get_num(buffer, 1, width) - 1;
  104.             }
  105.             while (!c4_make_move(turn, move, NULL));
  106.         }
  107.         else {
  108.             if (num_of_players == 1)
  109.                 printf("Thinking");
  110.             else
  111.                 printf("Player %c is thinking", piece[turn]);
  112.             fflush(stdout);
  113.             c4_poll(print_dot, level[turn] - 1);
  114.             c4_auto_move(turn, level[turn], &move, NULL);
  115.             if (num_of_players == 1)
  116.                 printf("\n\nI dropped my piece into column %d.\n", move+1);
  117.             else
  118.                 printf("\n\nPlayer %c dropped its piece into column %d.\n",
  119.                        piece[turn], move+1);
  120.         }
  121.  
  122.         turn = !turn;
  123.  
  124.     } while (!c4_is_winner(0) && !c4_is_winner(1) && !c4_is_tie());
  125.  
  126.     print_board(width, height);
  127.  
  128.     if (c4_is_winner(0)) {
  129.         if (num_of_players == 1)
  130.             printf("You won!");
  131.         else
  132.             printf("Player %c won!", piece[0]);
  133.         c4_win_coords(&x1, &y1, &x2, &y2);
  134.         printf("  (%d,%d) to (%d,%d)\n\n", x1+1, y1+1, x2+1, y2+1);
  135.     }
  136.     else if (c4_is_winner(1)) {
  137.         if (num_of_players == 1)
  138.             printf("I won!");
  139.         else
  140.             printf("Player %c won!", piece[1]);
  141.         c4_win_coords(&x1, &y1, &x2, &y2);
  142.         printf("  (%d,%d) to (%d,%d)\n\n", x1+1, y1+1, x2+1, y2+1);
  143.     }
  144.     else {
  145.         printf("There was a tie!\n\n");
  146.     }
  147.  
  148.     c4_end_game();
  149.     return 0;
  150. }
  151.  
  152.  
  153. /****************************************************************************/
  154.  
  155. static int
  156. get_num(char *prompt, int lower, int upper)
  157. {
  158.     int number;
  159.     static char numbuf[40];
  160.  
  161.     do {
  162.         printf("%s", prompt);
  163.         if (!fgets(numbuf, 40, stdin) || numbuf[0] == 'q') {
  164.             printf("\nGoodbye!\n");
  165.             exit(0);
  166.         }
  167.         sscanf(numbuf, "%d", &number);
  168.     } while (!isdigit(numbuf[0]) || number < lower || number > upper);
  169.  
  170.     return number;
  171. }
  172.  
  173. /****************************************************************************/
  174.  
  175. static void
  176. print_board(int width, int height)
  177. {
  178.     int x, y;
  179.     char **board, spacing[2], dashing[2];
  180.  
  181.     board = c4_board();
  182.  
  183.     spacing[1] = dashing[1] = '\0';
  184.     if (width > 19) {
  185.         spacing[0] = '\0';
  186.         dashing[0] = '\0';
  187.     }
  188.     else {
  189.         spacing[0] = ' ';
  190.         dashing[0] = '-';
  191.     }
  192.  
  193.     printf("\n");
  194.     for (y=height-1; y>=0; y--) {
  195.  
  196.         printf("|");
  197.         for (x=0; x<width; x++) {
  198.             if (board[x][y] == C4_NONE)
  199.                 printf("%s %s|", spacing, spacing);
  200.             else
  201.                 printf("%s%c%s|", spacing, piece[(int)board[x][y]], spacing);
  202.         }
  203.         printf("\n");
  204.  
  205.         printf("+");
  206.         for (x=0; x<width; x++)
  207.             printf("%s-%s+", dashing, dashing);
  208.         printf("\n");
  209.     }
  210.  
  211.     printf(" ");
  212.     for (x=0; x<width; x++)
  213.         printf("%s%d%s ", spacing, (x>8)?(x+1)/10:x+1, spacing);
  214.     if (width > 9) {
  215.         printf("\n ");
  216.         for (x=0; x<width; x++)
  217.             printf("%s%c%s ", spacing, (x>8)?'0'+(x+1)-((x+1)/10)*10:' ',
  218.                               spacing);
  219.     }
  220.     printf("\n\n");
  221. }
  222.  
  223. /****************************************************************************/
  224.  
  225. static void
  226. print_dot(void)
  227. {
  228.     printf(".");
  229.     fflush(stdout);
  230. }
  231.